package cn.anc.aonicardv.module.ui.recorder;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;
import butterknife.OnClick;
import cn.anc.aonicardv.Constant;
import cn.anc.aonicardv.R;
import cn.anc.aonicardv.R2;
import cn.anc.aonicardv.bean.PhotoBean;
import cn.anc.aonicardv.event.BigImageDataEvent;
import cn.anc.aonicardv.module.adpter.album.ImageBrowserPageAdapter;
import cn.anc.aonicardv.module.ui.MainActivity;
import cn.anc.aonicardv.module.ui.base.BaseActivity;
import cn.anc.aonicardv.util.DeviceInfo;
import cn.anc.aonicardv.util.DialogUtils;
import cn.anc.aonicardv.widget.ImageBrowserViewPager;
import cn.anc.aonicardv.widget.WarningDialog;
import cn.anc.httpcontrolutil.CarControl;
import cn.anc.httpcontrolutil.cache.CacheManager;

public class RecorderBigImageActivity extends BaseActivity implements CarControl.OnHeartBeatCallBack, View.OnClickListener, CarControl.OnCardvThumbnailCallback {

    @BindView(R2.id.ll_exit)
    LinearLayout llExit;
    @BindView(R2.id.tv_title_name)
    TextView tvTitle;

    @BindView(R2.id.vp)
    ImageBrowserViewPager mVp;
    private ImageBrowserPageAdapter mAdapter;
    private List<PhotoBean> photoBeanList;
    private int currentPosition;
    private ProgressDialog progressDialog;
    private WarningDialog warningDialog;
    private CarControl.CarDvThumbnailMediaInfo mediaInfo;
    private DialogUtils dialogUtils;
    private static String TAG = RecorderBigImageActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_recorder_big_image);
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void initVariable() {
        tvTitle.setVisibility(View.GONE);
        setLayoutSize(mVp, DeviceInfo.ScreenHeightPixels*16/9,DeviceInfo.ScreenHeightPixels);
        dialogUtils = new DialogUtils();
        progressDialog = dialogUtils.getProgressDialog(this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        warningDialog = dialogUtils.getWarningDialog(this);
        dialogUtils = new DialogUtils();
    }
    private void setLayoutSize(View view,int width, int height) {
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) view.getLayoutParams();
        lp.width = width;
        lp.height = height;
        view.setLayoutParams(lp);
    }

    @Override
    public void setListener() {
        warningDialog.okTv.setOnClickListener(this);
    }

    @Override
    public void initData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        h.postDelayed(animateViewPager, ANIM_VIEWPAGER_DELAY);
        CarControl.setOnHeartBeatCallBack(this);
        if (!CarControl.IsConnected.get()){
            if (null != warningDialog) {
                if (warningDialog.isShowing()) {
                    warningDialog.dismiss();
                }
                if (!warningDialog.isShowing() && !isDestroyed()) {
                    warningDialog.setHint(getString(R.string.tip_cardv_disconnect)).show();
                }

            }
        }

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onMessageEvent(BigImageDataEvent event) {
        photoBeanList = event.photoBeanList;
        currentPosition = Integer.valueOf(getIntent().getStringExtra(Constant.IntentKeyParam.POSITION));
        mAdapter = new ImageBrowserPageAdapter(this, photoBeanList, true);
        mVp.setAdapter(mAdapter);
        updateTitle(currentPosition);
        mVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                currentPosition = position;
                String path = photoBeanList.get(position).getPath();
                String cachePath = CacheManager.getCachePath(path);
                updateTitle(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        mVp.setCurrentItem(currentPosition);

    }
    @Override
    public void OnThumbnailGetted(int index) {
        CarControl.CarDvThumbnailMediaInfo mediaInfo = CacheManager.getMediaInfoFromExif(CacheManager.getCachePath(photoBeanList.get(currentPosition).getPath()));
        if (mediaInfo != null) {
            tvTitle.setVisibility(View.GONE);
            tvTitle.setText(mediaInfo.file_time);
        }
    }
    private void loadVideoThumbs(int position) {
        List<Integer> idxArray = new ArrayList<>();
        if (!CacheManager.checkCacheExist(photoBeanList.get(position).getPath())) {
            List<PhotoBean> mLoadBean = new ArrayList<>();
            mLoadBean.add(photoBeanList.get(position));
            idxArray.add(0);
            CarControl.doGetVideoThumbnail(mLoadBean, idxArray, RecorderBigImageActivity.this);
        }
    }
    private void updateTitle(int position) {
        CarControl.CarDvThumbnailMediaInfo mediaInfo = CacheManager.getMediaInfoFromExif(CacheManager.getCachePath(photoBeanList.get(position).getPath()));
        if (mediaInfo != null&&null!=tvTitle) {
            tvTitle.setVisibility(View.GONE);
            tvTitle.setText(mediaInfo.file_time);
        }else{
            loadVideoThumbs(position);
        }
    }

    @Override
    public void onClick(View v) {
        warningDialog.dismiss();
        startActivity(new Intent(this, MainActivity.class));
    }

    @OnClick(R2.id.ll_exit)
    public void back() {
        finish();
    }


    @Override
    public void OnHeartBeatConnectOK() {
    }

    @Override
    public void OnHeartBeatConnectFail() {
    }

    @Override
    public void OnHeartBeatDisconnect() {
        if (null != warningDialog&&!isDestroyed()){
            warningDialog.setHint(getString(R.string.tip_cardv_disconnect)).show();
        }
    }

    // viewpager auto play
    private static final long ANIM_VIEWPAGER_DELAY = 10000;

    private Handler h = new Handler();
    private Runnable animateViewPager = new Runnable() {
        public void run() {
            if (null!=mVp&&photoBeanList.size() > 0 && mVp.getCurrentItem() < photoBeanList.size() - 1) {
                mVp.setCurrentItem((mVp.getCurrentItem() + 1)
                        % photoBeanList.size(), true);
                h.postDelayed(animateViewPager, ANIM_VIEWPAGER_DELAY);
            } else {
                finish();
            }
        }
    };

    @Override
    public void onPause() {
        super.onPause();
        if (h != null) {
            h.removeCallbacks(animateViewPager);
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            if (h != null) {
                h.removeCallbacks(animateViewPager);
            }
        }
        if (ev.getAction() == MotionEvent.ACTION_UP) {
            h.postDelayed(animateViewPager, ANIM_VIEWPAGER_DELAY);
        }
        return super.dispatchTouchEvent(ev);
    }



}
